//公共二级弹层
(function () {
    function MailDetail(data, index) {
        MailDetail.super(this);
        this.width = 1334;
        this.height = Laya.stage.height;
        this._mailbox = null;
        this._mask = null;
        this._mailID = null;
        this._detailID = [];
        this._data = data;
        this._index = index;
        this.skin = null;

        //ppt.io.register("mail::readMail", this, this.mailpage);

        this.init(data, index);
    }
    //继承
    Laya.class(MailDetail, "Alert.MailDetail", Laya.Sprite);
    var _proto = MailDetail.prototype;
    _proto.destroy = function () {
        Laya.Sprite.prototype.destroy.call(this, true);
        this._mailbox = null;
        this._mask = null;
        this._mailID = null;
    }

    //初始化
    _proto.init = function (data, index) {
        var mailbox = new ui.Alert.MailDetailUI();
        mailbox.mailList.array = [];
        mailbox.x = Laya.stage.width / 2 - mailbox.width / 2;
        mailbox.y = Laya.stage.height / 2 - mailbox.height / 2;
        mailbox.zOrder = 4;
        this._mailbox = mailbox;
        this.addChild(mailbox);
        this._mask = new Laya.Label();
        this._mask.width = Laya.stage.width;
        this._mask.height = Laya.stage.height;
        this._mask.bgColor = '#000';
        this._mask.alpha = 0.6;
        this._mask.zOrder = 3;
        this._mask.mouseEnabled = true;
        this.addChild(this._mask);
        mailbox.mailtitle.text = data[index].title;
        mailbox.mailcontent.text = data[index].content;
        this._mailID = data[index].id;
        var dataDetail = data[index].attachment;
        var itemId;
        mailbox.mailList.width = (dataDetail.length) * 96 + (dataDetail.length - 1) * 15;
        mailbox.mailList.x = 410 - mailbox.mailList.width / 2;
        for (var i = 0; i < dataDetail.length; i++) {
            dataDetail[i].num = 'x' + dataDetail[i].count;
            mailbox.mailList.array = dataDetail;

        }
        mailbox.mailList.renderHandler = new laya.utils.Handler(this, this.listMail);
        mailbox.close.on(Laya.Event.CLICK, this, this.Close);
        mailbox.closeBtn.on(Laya.Event.CLICK, this, this.Closeback);
        mailbox.detailClose.on(Laya.Event.CLICK, this, this.detailClosebox);
        this._mask.on(Laya.Event.CLICK, this, this.detailClosebox);

        mailbox.mailList.mouseHandler = new laya.utils.Handler(this, this.onSelect);
    }
    _proto.listMail = function (mail, index) {
        if (this._data[this._index].attachment[index].itemId == 'honor') {
            this.skin = 'res/public/icon_star.png';
        }
        else if (this._data[this._index].attachment[index].itemId == 'diamond') {
            this.skin = 'res/public/icon_diamond.png';
        }
        else if (this._data[this._index].attachment[index].itemId == 'gold') {
            this.skin = 'res/public/icon_coin.png';
        }else{
            this.skin = 'res/props/' + this._data[this._index].attachment[index].itemId + '.png';
        }
        mail.getChildByName('trend').skin = this.skin;
    }
    _proto.Closeback = function () {
        this.destroy(true);
        ppt.io.emit({ "cmd": "mail::getMail" });
    }
    //选择邮件
    _proto.onSelect = function (e, index) {
        if (e.type == Laya.Event.CLICK) {
            if (this._data[this._index].attachment[index].itemId == 'honor' || this._data[this._index].attachment[index].itemId == 'diamond' || this._data[this._index].attachment[index].itemId == 'gold') {
                return;
            }
            this._mailbox.detailbox.visible = true;

            this._mailbox.detailbox.x = 410 - this._mailbox.mailList.width / 2 + 110 * index;
            this._mailbox.detailpic.skin = 'res/props/' + this._data[this._index].attachment[index].itemId + '.png';
            this._mailbox.detailtitle.text = this._data[this._index].attachment[index].name;
            this._mailbox.detailcontent.text = this._data[this._index].attachment[index].desc;
        }
    }
    _proto.detailClosebox = function (e) {
        this._mailbox.detailbox.visible = false;
    }
    _proto.Close = function () {
        this.destroy(true);
    }
})();
